using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour {
	public enum AUDIOTYPE {
											START_GAME,
											GENERATE_BIRD,
											GENERATE_ENEMY,
											FALL_DOWN , 
											HIT ,
											INVADED,
											GET_ITEM,
											};
	public static SoundManager Instance;
	protected AudioSource m_audioSource;

	//****************************************************************************************************
	//		Some clips are here, some are not for the player and the enemy has an static clip that didn't 
	//need to storage in its member variable.
	//****************************************************************************************************
	public AudioClip audioBGM;										//background music.
	public AudioClip audioStartGame;								//The clip to play when click start.
	public AudioClip audioGenerateBird;							//The clip to play when generate a bird.
	public AudioClip audioGenerateEnemy;						//Enemy generate.

	public AudioClip audioFalldown;								//The bird was defended.
	public AudioClip audioHit;										//Hit the bad guy
	public AudioClip audioInvaded;								//The bad invaded into the forest.

	public AudioClip audioGetItem;								//Get item.

	// Use this for initialization
	void Start () {
		m_audioSource = this.GetComponent<AudioSource>();
		Instance = this;


		//Play the background music;
		m_audioSource.clip = audioBGM;
		m_audioSource.loop = true;
		if (PlayerPrefs.GetInt("BGM_MUSIC") == 0)
		{	
			m_audioSource.Play();
		}
		else
		{
			m_audioSource.Stop();
		}
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void PlayAudio(AUDIOTYPE audioType)
	{
		if (m_audioSource.isPlaying)
		{
			switch (audioType)
			{
				case AUDIOTYPE.START_GAME:
					m_audioSource.PlayOneShot(audioStartGame);
					break;

				case AUDIOTYPE.GENERATE_BIRD:
					m_audioSource.PlayOneShot(audioGenerateBird);
					break;

				case AUDIOTYPE.GENERATE_ENEMY:
					m_audioSource.PlayOneShot(audioGenerateEnemy);
					break;

				case AUDIOTYPE.FALL_DOWN:
					m_audioSource.PlayOneShot(audioFalldown);
					break;

				case AUDIOTYPE.HIT:
					m_audioSource.PlayOneShot(audioHit);
					break;

				case AUDIOTYPE.INVADED:
					m_audioSource.PlayOneShot(audioInvaded);
					break;

				case AUDIOTYPE.GET_ITEM:
					m_audioSource.PlayOneShot(audioGetItem);
					break;
				default:
					Debug.LogError("try to play an undefine music !");
					break;
			}
		}
	}

	public void TurnOffMusic()
	{
		if (m_audioSource.isPlaying)
		{
			m_audioSource.Stop();
			PlayerPrefs.SetInt("BGM_MUSIC" , -1);
			PlayerPrefs.Save();
		}
	}
	public void TurnOnMusic()
	{
		if (!m_audioSource.isPlaying)
		{
			m_audioSource.Play();
			PlayerPrefs.SetInt("BGM_MUSIC" , 0);
			PlayerPrefs.Save();
		}
	}

	public bool IsPlayingBGM()
	{
		return m_audioSource.isPlaying;
	}
}
